package com.schoolhelper.model.dao.okhttp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.schoolhelper.model.Model;
import com.schoolhelper.model.dao.jsonparse.JsonParser;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import lombok.SneakyThrows;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RefactorRequestUrl {

    @SneakyThrows
    public static String getGetResponseJson(String baseUrl, String api, Map<String, String> headers,Object requestBody) {

        OkHttpClient client = OkHttpUtil.getInstance();
        String url = requestBody == null ? baseUrl + "/" + api: baseUrl + "/" + api + "/" + requestBody;
        Request request = new Request.Builder()
                .url(url)
                .headers(Headers.of(headers))
                .get()
                .build();

        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                Response response = client.newCall(request).execute();
                return response.body().string();
            }
        };
        Future<String> response = Model.getInstance().getGlobalThreadPool().submit(callable);
        return response.get();
    }

    @SneakyThrows
    public static String getPostResponseJson(String baseUrl, String api, Map<String, String> headers, Object requestBody) {
        String json = JSON.toJSONString(requestBody, SerializerFeature.WriteMapNullValue);
        OkHttpClient client = OkHttpUtil.getInstance();
        Request request = new Request.Builder()
                .url(baseUrl + "/" + api)
                .headers(Headers.of(headers))
                .post(RequestBody.create(MediaType.parse("application/json"), json))
                .build();

        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                Response response = client.newCall(request).execute();
                return response.body().string();
            }
        };
        Future<String> response = Model.getInstance().getGlobalThreadPool().submit(callable);
        return response.get();
    }

    @SneakyThrows
    public static <T> List<T> postThenGetList(String baseUrl, String api, Map<String, String> headers, T requestBody,
                                              Class<T> resultVo, JsonParser jsonParser) {


        String responseData = getPostResponseJson(baseUrl, api, headers, requestBody);
        return jsonParser.getList(responseData, resultVo);
    }
    @SneakyThrows
    public static <T> T postThenGetOne(String baseUrl, String api, Map<String, String> headers, Object requestBody,
                                              Class<T> resultVo, JsonParser jsonParser) {

        String responseData = getPostResponseJson(baseUrl, api, headers, requestBody);
        return jsonParser.getOne(responseData, resultVo);
    }

    @SneakyThrows
    public static <T> boolean justPost(String baseUrl, String api, Map<String, String> headers, T requestBody,
                                       JsonParser jsonParser) {

        String responseData = getPostResponseJson(baseUrl, api, headers, requestBody);
        return jsonParser.getCode(responseData);
    }

    @SneakyThrows
    public static <T> List<T> getThenGetList(String baseUrl, String api, Map<String, String> headers, T requestBody,
                                     Class<T> resultVo, JsonParser jsonParser) {

        String responseData = getGetResponseJson(baseUrl, api, headers, requestBody);
        return jsonParser.getList(responseData, resultVo);
    }

    @SneakyThrows
    public static <T> T getThenGetOne(String baseUrl, String api, Map<String, String> headers, T requestBody,
                                            Class<T> resultVo, JsonParser jsonParser) {

        String responseData = getGetResponseJson(baseUrl, api, headers, requestBody);
        return jsonParser.getOne(responseData, resultVo);

    }

}
